package net.chrisrichardson.ec2deployer.applications

import net.chrisrichardson.ec2deployer.core.ProcessMonitor

class StdoutCapturingProcessMonitor implements ProcessMonitor {

    Process process
    PrintWriter writer

    StdoutCapturingProcessMonitor(Writer writer) {
        this.writer = new PrintWriter(writer)
    }

    void monitorProcess(Process p) {
        this.process = p
        Thread.startDaemon {readProcessIn(p.in)}
        Thread.startDaemon {System.err << p.err}
    }

    void readProcessIn(is) {
        String line
        InputStreamReader isr = new InputStreamReader(is)
        while ((line = isr.readLine()) != null) {
            writer.println line
        }
        writer.flush()
    }

    public void noteCommand(String command) {
    }

    public boolean isWaitForOrKillRequired() {
        return true
    }

    public void noteEndedOrTimedOut(Process process) {
    }
}